package com.hochschild.sgv.util;


import com.hochschild.sgv.domain.Conexion;
import com.sun.rowset.CachedRowSetImpl;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sql.rowset.CachedRowSet;

public class ConexionBD {
    
    private Connection con=null;
    private Statement stm=null;
    private ResultSet rst=null;    

    public ConexionBD(){       
        try {
            Conexion bean = new Conexion();
            Class.forName(bean.getStrDriver());           
            String strUrl = bean.getStrUrl();
            String strUsuario = bean.getStrUsuario();
            String strPassword = bean.getStrPassword();
            con = DriverManager.getConnection(strUrl,strUsuario,strPassword);
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

     
    public Connection getConnection(){
        return con;
    }
    
    public ResultSet ExecuteSelect( String SQL, String clase){
//      ResultSet rst =  new ConexionBD().ExecuteSelect(SQL, "SimplePlanificacionManager");
        System.out.println("Execute Select");
        Connection conn = null;
        ConexionBD conBD = null;
        PreparedStatement pstm = null;
        ResultSet rst = null;
        CachedRowSet cachedRowSet=null;
        try {
            conBD = new ConexionBD();
            conn = conBD.getConnection();
                System.out.println("SQL: " + SQL.toString());
            pstm = conn.prepareStatement(SQL.toString());
            rst = pstm.executeQuery();

            cachedRowSet = new CachedRowSetImpl();
            cachedRowSet.populate(rst);
           
       } catch (Exception e) {
            e.printStackTrace();
       }finally {
            if (pstm != null) {
                try {
                    pstm.close();
                } catch (SQLException ex) {
                    Logger.getLogger(clase);
                }
            }
            if (rst != null) {
                try {
                    rst.close();
                } catch (SQLException ex) {
                    Logger.getLogger(clase);
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException ex) {
                    Logger.getLogger(clase);
                }
            }
        }

       return cachedRowSet;
   }
    
}
